package com.yami.shop.rights.common.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.rights.common.dao.RightsVerificationUserMapper;
import com.yami.shop.rights.common.dto.RightsVerificationUserDto;
import com.yami.shop.rights.common.model.ProdRightsExternalData;
import com.yami.shop.rights.common.model.RightsVerificationUser;
import com.yami.shop.rights.common.service.ProdRightsExternalDataService;
import com.yami.shop.rights.common.service.RightsVerificationUserService;
import com.yami.shop.security.common.util.AuthUserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RightsVerificationUserServiceImpl extends ServiceImpl<RightsVerificationUserMapper, RightsVerificationUser> implements RightsVerificationUserService {

    @Autowired
    private RightsVerificationUserMapper rightsVerificationUserMapper;
    @Autowired
    private ProdRightsExternalDataService prodRightsExternalService;

    @Override
    public IPage<RightsVerificationUser> pageList(PageParam<RightsVerificationUser> page, RightsVerificationUserDto vo) {
        QueryWrapper<RightsVerificationUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shop_id",vo.getShopId());
        queryWrapper.ne("status",-1);
        return rightsVerificationUserMapper.selectPage(page,queryWrapper);
    }

    @Override
    public boolean isVerificationRights(Long shopId) {
        String userId = AuthUserContext.get().getUserId();
        // 查询该人员是否拥有核销权限
        QueryWrapper<RightsVerificationUser> verificationUserQueryWrapper = new QueryWrapper<>();
        verificationUserQueryWrapper.eq("shop_id",shopId);
        verificationUserQueryWrapper.eq("user_id",userId);
        verificationUserQueryWrapper.eq("status","1");
        RightsVerificationUser rightsVerificationUser = rightsVerificationUserMapper.selectOne(verificationUserQueryWrapper);
        return ObjectUtil.isNotEmpty(rightsVerificationUser);
    }

    @Override
    public boolean isVerificationRightsByUserId(String userId) {
        // 查询该人员是否拥有核销权限
        QueryWrapper<RightsVerificationUser> verificationUserQueryWrapper = new QueryWrapper<>();
        verificationUserQueryWrapper.eq("user_id",userId);
        verificationUserQueryWrapper.eq("status","1");
        List<RightsVerificationUser> rightsVerificationUsers = rightsVerificationUserMapper.selectList(verificationUserQueryWrapper);
        return ObjectUtil.isNotEmpty(rightsVerificationUsers);
    }
}
